using System;
using System.Collections.Generic;

using System.IO;
namespace Microsoft.Health.Samples.PatientConnect.ContosoHealth.HealthVaultAdapter
{
    /// <summary>
    /// This class handles the logging part for scheduler
    /// </summary>
    public class Logger
    {
        /// <summary>
        /// Logs information about scheduler's last run time.
        /// </summary>        
        public static void LogSchedulerLastRun()
        {
            string fileName = String.Empty;
            fileName = SchedulerConfig.LastRunLogFile;
            LogWriter(fileName, DateTime.Now.ToString(), true);
        }

        /// <summary>
        /// Logs the event into a log file.
        /// </summary>
        /// <param name="message">Message to be logged.</param>
        /// <param name="status">Status of the scheduler</param>
        public static void LogEvent(string message, 
                                    SchedulerStatus status)
        {
            string fileName = string.Empty;
            string messageToWrite = string.Empty;

            fileName = SchedulerConfig.LogFile;
            switch (status)
            {
                case SchedulerStatus.Started:
                case SchedulerStatus.Stopped:
                case SchedulerStatus.Error:
                case SchedulerStatus.Running:
                    messageToWrite=string.Format("{0} {1}",message,Environment.NewLine);
                    break;

            }
            LogWriter(fileName, messageToWrite.ToString(), false);
        }

        /// <summary>
        /// Write the message to the specified file.
        /// </summary>
        /// <param name="filePath">Log file name</param>        
        /// <param name="message">Message to be logged.</param>
        /// <param name="overwrite">Overwrite the file, if true overwrite, if false append the log file.</param>
        public static void LogWriter(string filePath, 
                                     string message,
                                     bool overwrite)
        {
            using (StreamWriter writeToLog = new StreamWriter(filePath, !overwrite))
            {
                writeToLog.WriteLine(message);
                writeToLog.Flush();
            }
        }
    }
    /// <summary>
    /// Status of the scheduler
    /// </summary>
    public enum SchedulerStatus
    {
        /// <summary>
        /// Service started
        /// </summary>
        Started,
        /// <summary>
        /// Service stopped
        /// </summary>
        Stopped,
        /// <summary>
        /// Error occurred in service
        /// </summary>
        Error,
        /// <summary>
        /// Service running
        /// </summary>
        Running
    }
}